package org.dromara.llm.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.llm.domain.UploadedFiles;
import org.dromara.llm.domain.vo.UploadedFilesVo;
import org.dromara.llm.service.IUploadedFilesService;
import org.dromara.llm.stt.SttService;
import org.dromara.llm.stt.factory.SttServiceFactory;
import org.dromara.llm.stt.providers.SensorySttService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 语言识别
 *
 * @author Miles Meng
 * @date 2025-06-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/llm/stt")
public class SttController extends BaseController {
    private final SensorySttService sensorySttService;
    private final IUploadedFilesService uploadedFilesService;
    private final SttServiceFactory sttServiceFactory;

    /**
     * 上传语言文件识别
     */
    @SaIgnore
    @PostMapping("/uploadFileBySensory")
    public R<UploadedFiles> uploadFileBySensory(@RequestParam("file") MultipartFile file, @RequestParam("lang") String lang) {
        UploadedFiles result = sensorySttService.recognition(file, lang);
        return R.ok(result);
    }

    /**
     * 获取上传文件详细信息
     *
     * @param id 主键
     */
    @SaIgnore
    @GetMapping("/{id}")
    public R<UploadedFilesVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable String id) {
        return R.ok(uploadedFilesService.queryById(id));
    }

    /**
     * 上传文件
     */
    @SaIgnore
    @PostMapping("/uploadFileByVosk")
    public R<String> uploadFileByVosk(@RequestParam("file") MultipartFile file, @RequestParam("lang") String lang) throws IOException {
        SttService sttService = sttServiceFactory.getDefaultSttService();
        String result = sttService.recognition(file.getInputStream().readAllBytes());
        return R.ok(result);
    }

    /**
     * 上传文件
     */
    @SaIgnore
    @PostMapping("/uploadFileByXf")
    public R<String> uploadFileByXf(@RequestParam("file") MultipartFile file) throws IOException {
        SttService sttService = sttServiceFactory.getDefaultSttService();
        String result = sttService.recognition(file.getInputStream().readAllBytes());
        return R.ok(result);
    }

}
